package com.stylefeng.guns.modular.salesmanagement.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.google.common.collect.Maps;
import com.stylefeng.guns.core.datascope.DataScope;
import com.stylefeng.guns.core.shiro.ShiroKit;
import com.stylefeng.guns.core.util.DateUtil;
import com.stylefeng.guns.modular.dto.GoodsSaleAddRequest;
import com.stylefeng.guns.modular.dto.SaleGoodsQueryRequest;
import com.stylefeng.guns.modular.manager.service.IManagerGoodsService;
import com.stylefeng.guns.modular.salesmanagement.service.IGoodsSaleListService;
import com.stylefeng.guns.modular.system.model.GoodsSale;
import com.stylefeng.guns.modular.system.dao.GoodsSaleMapper;
import com.stylefeng.guns.modular.salesmanagement.service.IGoodsSaleService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.stylefeng.guns.modular.system.model.GoodsSaleList;
import com.stylefeng.guns.modular.system.model.ManagerGoods;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collector;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author LiuXin
 * @since 2018-09-12
 */
@Service
public class GoodsSaleServiceImpl extends ServiceImpl<GoodsSaleMapper, GoodsSale> implements IGoodsSaleService {

    @Autowired
    private IGoodsSaleListService goodsSaleListService;
    /**
     * 库存服务
     */
    @Autowired
    private IManagerGoodsService managerGoodsService;

    @Override
    public boolean insertGoodSaleAndGoods(GoodsSaleAddRequest request) {
        GoodsSale goodsSale = new GoodsSale();
        BeanUtils.copyProperties(request, goodsSale);
        boolean success = this.insert(goodsSale);
        //先取出商品id和本次要减去的商品数量
        Map<Long, Integer> tempMap = Maps.newHashMap();
        request.getSaleGoodsList().forEach(goodsSaleList -> {
            goodsSaleList.setGoodsSaleId(goodsSale.getId());
            tempMap.put(goodsSaleList.getGoodsId(), goodsSaleList.getGoodsNum());
        });
        //更新减去库存数量
        managerGoodsService.batchMinusGoodsInventory(tempMap);
        boolean insertGoodsSaleListSuccess = goodsSaleListService.insertBatch(request.getSaleGoodsList());
        return success && insertGoodsSaleListSuccess;
    }

    /**
     * 删除了销售订单，那么库存数量需更新也即增加
     * @param goodsSaleId 订单id
     * @return
     */
    @Override
    public boolean deleteGoodSaleAndGoods(Long goodsSaleId) {
        EntityWrapper<GoodsSaleList> entityWrapper = new EntityWrapper<>();
        entityWrapper.eq("goods_sale_id", goodsSaleId);
        List<GoodsSaleList> goodsSaleList = goodsSaleListService.selectList(entityWrapper);
        //更新库存 增加数量
        Map<Long,Integer> tempMap = goodsSaleList.stream().collect(Collectors.toMap(GoodsSaleList::getGoodsId,GoodsSaleList::getGoodsNum));
        managerGoodsService.batchAddGoodsInventory(tempMap);
        boolean success = goodsSaleListService.delete(entityWrapper);
        boolean deleteGoodsSale = this.deleteById(goodsSaleId);
        return success && deleteGoodsSale;
    }

    @Override
    public Page<GoodsSale> selectPageWithDataScope(Page<GoodsSale> page, SaleGoodsQueryRequest request) {
        EntityWrapper<GoodsSale> entityWrapper = new EntityWrapper<>();
        //订单号
        if(StringUtils.isNotBlank(request.getSaleOrderNumber())){
            entityWrapper.eq("sale_order_number", request.getSaleOrderNumber());
        }

        //是否支付
        if(StringUtils.isNotBlank(request.getPayState())){
            entityWrapper.eq("pay_state", Integer.valueOf(request.getPayState()));
        }

        //开始日期 大于等于
        if(StringUtils.isNotBlank(request.getbDate())){
            entityWrapper.ge("create_date", DateUtil.parse(request.getbDate(),"yyyy-MM-dd"));
        }

        //结束日期 小于等于
        if(StringUtils.isNotBlank(request.geteDate())){
            entityWrapper.le("create_date", DateUtil.parse(request.geteDate(),"yyyy-MM-dd"));
        }

        //根据备注信息查询
        if(StringUtils.isNotBlank(request.getRemarks())){
            entityWrapper.like("remarks", request.getRemarks());
        }

        if(ShiroKit.isAdmin()){
            Page<GoodsSale> result = this.selectPage(page, entityWrapper);
            return result;
        }else {
            DataScope dataScope = new DataScope(ShiroKit.getDeptDataScope());
            List<GoodsSale> result = this.baseMapper.selectPageWithDataScope(page,dataScope,request);
            page.setRecords(result);
            return page;
        }
    }
}
